/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-22 17:37:14
 ******************************************************************************/
package cn.com.xy.sms.sdk.ui.popu.util;

import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.log.LogManager;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;

public class ColorParam {

    public int startColor = 1;// 开始颜色
    public int centerColor = 1;// 中间颜色
    public int endColor = 1;// 结束颜色
    public int angle = 0;// 角度0-7,步长45度
    public float cornersTopLeft = 0;// 圆角头
    public float cornersTopRight = 0;// 圆角头
    public float cornersBottomLeft = 0;// 圆角底
    public float cornersBottomRight = 0;// 圆角底
    public int type = 0; // 0:矩形,1:椭圆 2:线 3:圆
    public int strokeColor = 1;
    public int strokeWidth = 0;
    public int dashWidth = 0;
    public int dashGap = 0;
    public int width = 0;
    public int height = 0;
    public int gCenterX = 0;
    public int gCenterY = 0;
    public int gType = -1;
    public int gRadius = 0;

    // public int paddingLeft=0;
    // public int paddingRight=0;
    // public int paddingTop=0;
    // public int paddingBottom=0;

    /**
     * 
     * @param ctx
     * @param str
     *            各项之间;隔开 S 起始颜色 C 中间色 E 结束色 A 角度 TL 上方左边圆角 TR 上方右边圆角 BL 下方左边圆角
     *            BR 下方右边圆角 TP 类型 SC 描边色 SW 描边宽 DW 描边虚线宽度 DG 描边虚线间隔 W 宽 H 高 PL
     *            左间 PR 右间 PT 上间 PB 下间 CX GCenterX CY GCenterY GT gType GR
     *            gRadius
     * @return
     */
    public static ColorParam parseColorParamStr(Context ctx, String str) {

        ColorParam cp = null;
        try {
            if (str != null) {
                cp = new ColorParam();
                String[] param = str.split(";");
                for (String p : param) {
                    if (p.startsWith("TL")) {// 上方圆角
                        cp.cornersTopLeft = ViewUtil.dp2px(ctx,
                                Integer.valueOf(p.substring(2)));
                    } else if (p.startsWith("TR")) {
                        cp.cornersTopRight = ViewUtil.dp2px(ctx,
                                Integer.valueOf(p.substring(2)));
                    } else if (p.startsWith("BL")) {
                        cp.cornersBottomLeft = ViewUtil.dp2px(ctx,
                                Integer.valueOf(p.substring(2)));
                    } else if (p.startsWith("BR")) {// 下方圆角
                        cp.cornersBottomRight = ViewUtil.dp2px(ctx,
                                Integer.valueOf(p.substring(2)));
                    } else if (p.startsWith("TP")) {// 类型
                        cp.type = Integer.valueOf(p.substring(2));
                    } else if (p.startsWith("SC")) {// 描边色
                        cp.strokeColor = Color.parseColor(p.substring(2));
                    } else if (p.startsWith("SW")) {// 描边宽
                        cp.strokeWidth = Integer.valueOf(p.substring(2));
                    } else if (p.startsWith("DW")) {// 描边虚线宽度
                        cp.dashWidth = Integer.valueOf(p.substring(2));
                    } else if (p.startsWith("DG")) {// 描边虚线间隔
                        cp.dashGap = Integer.valueOf(p.substring(2));
                        // }else if(p.startsWith("PB")){//下间
                        // cp.paddingBottom=ContentUtil.dp2px(ctx,Integer.valueOf(p.substring(2)));
                        // }else if(p.startsWith("PT")){//下间
                        // cp.paddingTop=ContentUtil.dp2px(ctx,Integer.valueOf(p.substring(2)));
                        // }else if(p.startsWith("PR")){//下间
                        // cp.paddingRight=ContentUtil.dp2px(ctx,Integer.valueOf(p.substring(2)));
                        // }else if(p.startsWith("PL")){//左间
                        // cp.paddingLeft=ContentUtil.dp2px(ctx,Integer.valueOf(p.substring(2)));
                    } else if (p.startsWith("CX")) {// centerX
                        cp.gCenterX = ViewUtil.dp2px(ctx,
                                Integer.valueOf(p.substring(2)));
                    } else if (p.startsWith("CY")) {// centerY
                        cp.gCenterY = ViewUtil.dp2px(ctx,
                                Integer.valueOf(p.substring(2)));
                    } else if (p.startsWith("GT")) {// gType
                        cp.gType = Integer.valueOf(p.substring(2));
                    } else if (p.startsWith("GR")) {// gRadius
                        cp.gRadius = ViewUtil.dp2px(ctx,
                                Integer.valueOf(p.substring(2)));
                    } else if (p.startsWith("W")) {// 宽
                        cp.width = ViewUtil.dp2px(ctx,
                                Integer.valueOf(p.substring(1)));
                    } else if (p.startsWith("H")) {// 高
                        cp.height = ViewUtil.dp2px(ctx,
                                Integer.valueOf(p.substring(1)));
                    } else if (p.startsWith("S")) {// 起始色
                        cp.startColor = Color.parseColor(p.substring(1));
                    } else if (p.startsWith("C")) {// 中间色
                        cp.centerColor = Color.parseColor(p.substring(1));
                    } else if (p.startsWith("E")) {// 结束色
                        cp.endColor = Color.parseColor(p.substring(1));
                    } else if (p.startsWith("A")) {// 角度
                        cp.angle = Integer.valueOf(p.substring(1));
                    }
                }
            }
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "parseColorParamStr: ", e);

        }
        return cp;
    }

    // public static Drawable getDrawable(){
    // ColorParam cp = new ColorParam();
    // cp.angle=2;
    // cp.type=0;
    // cp.startColor=Color.BLUE;
    // //cp.centerColor=Color.BLACK;
    // //cp.endColor=Color.RED;
    // cp.strokeWidth=0;
    // //cp.strokeColor=Color.CYAN;
    // cp.dashGap=2;
    // cp.dashWidth=2;
    // return cp.getGDrawable();
    //
    // ColorParam cp = ColorParam.parseColorParamStr(mContext,
    // "SC#a8fe01;SW3;CT8;CB8;S#ff0000;E#FF00FF;A2;DW");
    // ColorParam cp = ColorParam.parseColorParamStr(mContext,
    // "CT8;CB8;S#ff0000;C#00FF00;E#FF00FF;A6");
    // ColorParam cp = ColorParam.parseColorParamStr(mContext,
    // "CT8;CB8;SC#a8fe01;SW3;DW3;DG3");

    // }
    public Drawable getGDrawable() {
        try {

            Orientation orType = Orientation.LEFT_RIGHT;
            try {
                switch (angle) {// 角度
                case 0:
                    orType = Orientation.LEFT_RIGHT;// 左到右
                    break;
                case 1:
                    orType = Orientation.RIGHT_LEFT;// 右到左
                    break;
                case 2:
                    orType = Orientation.TOP_BOTTOM;// 上到下
                    break;
                case 3:
                    orType = Orientation.BOTTOM_TOP;// 下到上
                    break;
                case 4:
                    orType = Orientation.TL_BR;// 上左下右
                    break;
                case 5:
                    orType = Orientation.TR_BL;// 上右下左
                    break;
                case 6:
                    orType = Orientation.BL_TR;// 下左上右
                    break;
                case 7:
                    orType = Orientation.BR_TL;// 下右上左
                    break;
                }
            } catch (Throwable e) {
//                e.printStackTrace();
                LogManager.e(Constant.TAG, "getGDrawable: ", e);

            }

            // 颜色
            int[] colors = null;
            if (centerColor != 1 && startColor != 1 && endColor != 1) {
                colors = new int[] { startColor, centerColor, endColor };
            } else {
                if (endColor != 1 && startColor != 1) {
                    colors = new int[] { startColor, endColor };
                } else {
                    // gd.setColor(startColor);
                }
            }
            GradientDrawable gd = null;
            try {
                if (colors != null) {
                    gd = new GradientDrawable(orType, colors);
                    gd.setShape(type);// 形状
                } else {
                    if (startColor != 1) {
                        gd = new GradientDrawable();
                        gd.setColor(startColor);
                    }

                }

            } catch (Throwable e) {
//                e.printStackTrace();
                LogManager.e(Constant.TAG, "getGDrawable: ", e);

            }
            if (gd == null) {
                return null;
            }

            // 圆角
            gd.setCornerRadii(new float[] { cornersTopLeft, cornersTopLeft,
                    cornersTopRight, cornersTopRight, cornersBottomLeft,
                    cornersBottomLeft, cornersBottomRight, cornersBottomRight });

            // 描边
            if (strokeColor != 1) {
                gd.setStroke(strokeWidth, strokeColor, dashWidth, dashGap);
            }
            // 宽高
            if (width > 0 && height > 0) {
                gd.setSize(width, height);
            }
            //
            if (gCenterX > 0 && gCenterY > 0) {
                gd.setGradientCenter(gCenterX, gCenterY);
            }
            if (gType >= 0) {
                gd.setGradientType(gType);
            }
            if (gRadius > 0) {
                gd.setGradientRadius(gRadius);
            }
            return gd;
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "getGDrawable: ", e);

        }
        return null;
    }
}
